
package ch.hearc.securiteSysteme.serie2.ex3Dictionnaire.use;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

import org.kohsuke.args4j.CmdLineException;

public class UseDictionnaire
	{
	
	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/
	
	public static void main(String[] args)
		{
		Parameters t = new Parameters();
		try
			{
			t.exec(args);
			}
		catch (CmdLineException e1)
			{
			e1.printStackTrace();
			System.exit(-1);
			}
		
		char[] chars = t.getStrAlphabet().toCharArray();
		
		int m = Integer.parseInt(t.getMin());
		int p = Integer.parseInt(t.getMax());
		Assert.assertTrue(m <= p);
		
		List<String> listResult = new ArrayList<String>();
		for(int i = m; i <= p; i++)
			{
			iterate(chars, i, new char[i], 0, listResult);
			}
		
		System.out.println("Size = " + listResult.size());
		try
			{
			saveIO(t.getOutputFilename(), listResult);
			}
		catch (IOException e)
			{
			e.printStackTrace();
			}
		}
	
	private static void saveIO(String nameFile, List<String> list) throws IOException
		{
		FileOutputStream fileOutputStream = new FileOutputStream(nameFile);
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
		PrintWriter printWriter = new PrintWriter(bufferedOutputStream);
		
		for(String data:list)
			{
			printWriter.write(data + "\n\r");
			}
		
		printWriter.close();
		bufferedOutputStream.close();
		fileOutputStream.close();
		}
	
	public static void iterate(char[] chars, int len, char[] build, int pos, List<String> list)
		{
		if (pos == len)
			{
			String word = new String(build);
			list.add(word);
			return;
			}
		
		for(int i = 0; i < chars.length; i++)
			{
			build[pos] = chars[i];
			iterate(chars, len, build, pos + 1, list);
			}
		}
	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/
	}
